<?php
namespace App\Services\Admin;

use App\Models\User as UserModels;
use App\Models\UserIntegralChangeRecord as UserIntegralChangeRecordModels;
use App\Models\UserRank;
use Illuminate\Support\Facades\DB;

Class User extends Base
{
    public static function lists()
    {
        $where = [];

        $sear_value = request()->sear_value??'';

        if($sear_value != ''){
            $where[] = ['nickname','like','%'.$sear_value.'%'];
            $where[] = ['user_number','like','%'.$sear_value.'%','OR'];
            $where[] = ['phone','like','%'.$sear_value.'%','OR'];
            $where[] = ['name','like','%'.$sear_value.'%','OR'];
        }

        return UserModels::with('user_level')->where($where)->orderBy('id', 'desc');
    }


    public static function update_expire_time($param = [])
    {
        $expire_time = "{$param['expire_time']} 23:59:59";
        return (new UserModels())->where('id', $param['user_id'])->update(['expire_time'=>$expire_time]);
    }

    public static function info($id)
    {
        return (new UserModels)->where('id', $id)->first();
    }

    public static function info_user_number($where = [])
    {
        return (new UserModels)->where($where)->first();
    }

    public static function edit($id , $param=[])
    {
        return (new UserModels)->where('id',$id)->update($param);
    }

    /**
     * @param int $user_id
     * @param string $start_time 开始时间
     * @param string $end_time   结束时间
     * @return array
     */
    public static function integral_change_record($user_id = 0)
    {
        if(empty($user_id))return [];

        $result = (new UserIntegralChangeRecordModels)->where('user_id', $user_id);

        if(!empty(request()->start_time) && !empty(request()->end_time)){
            $result = $result->where('created_at','>=', request()->start_time)->where('created_at', '<=', request()->end_time);
        }

        if(!empty(request()->chage_type)){
            $result = $result->where('chage_type',request()->chage_type);
        }

        return $result;
    }

    /**
     * 积分操作
     * @param $user_id  用户id
     * @param $reason   备注原因
     * @param $type     加积分类型 1：添加  5: 减分
     * @param int $record 操作积分
     */
    public static function record_do($user_id, $reason, $type, $record)
    {
        //开启事务
        DB::beginTransaction();
        try{

            $user = (new UserModels())->where('id', $user_id);

            switch ($type){
                case 1:
                    $user->increment('total_integral', $record);
                    break;
                case 5:
                    $user->decrement('total_integral', $record);
                    break;
            }

            $user_integral = (new UserIntegralChangeRecordModels);
            $user_integral->user_id = $user_id;
            $user_integral->reason = $reason;
            $user_integral->chage_type = $type;
            $user_integral->use_integral = $record;
            $user_integral->integral = $user->first()->total_integral;
            $user_integral->save();

            DB::commit();

        }catch (\Exception $e) {
            //接收异常处理并回滚
            DB::rollBack();
        }

    }

    /**
     * 用户修改等级
     * @param $user_id
     * @param $rank_id
     */
    public static function edit_rank($user_id , $rank_id)
    {
        $user_rank = (new UserRank)->where('id', $rank_id)->first();

        $user = UserModels::where('id',$user_id)->first();
        $user->user_rank_id = $rank_id;
        $user->user_rank_level = $user_rank->level;
        $user->save();
    }
}
